#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/un.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <string.h>

#define  BUFSIZE            (10)
#define  ADRESS             "127.0.0.1"
#define  PORT               (9734)
#define  OUT

static int SocketNew(int* _socket,struct sockaddr_in* _saddr,const char* _name)
{
    *_socket = socket( AF_INET /*1st argument is the purpose AF_UNIX local*/ ,
                SOCK_STREAM /*communication sematics SOCK_STREAM ==two way connect*/,
                0/*Protocol*/);

    if(*_socket ==-1)
    {
        perror("socket");
        return 1;
    }
    _saddr->sin_family = AF_INET;/*dueto AF_UNIX unix(7) instructions*/
    _saddr->sin_addr.s_addr = inet_addr(ADRESS);/*dueto AF_UNIX unix(7) instructions)*/
    _saddr->sin_port = htons(PORT);/*convert byte representation on number to network represent*/
    return 0;
}

/*Add to the massage Header at sizeof int + 1 and send to server*/
static void AddHeader(char* buf1,char* buf2)
{
    *(int*)buf2 = strlen(buf1)+1+sizeof(int);
    strcat((buf2 + sizeof(int)),buf1);
    return;
}

int main()
{
    int                 client_sockfd;
    struct sockaddr_in  client_saddr;

    int                 err;
    char                buf[BUFSIZE]={0};
    char*               sbuf = "Ping";
    char*               soob = "Warrning";
    char                headbufer[100];
    char*               ch;
    int                 wr = 0 ;
    int                 r = 0;
    int                 flag;

/*     unlink("serv_sock");/*delete the named socket*/
    if(SocketNew(&client_sockfd,&client_saddr,0) )
    {
        fprintf(stderr,"\nSocket New problems");
        exit(EXIT_FAILURE);
    }

/*    close(client_sockfd);*/

    /*creation of connecton*/
    {
    err = connect(client_sockfd,(struct sockaddr*)&client_saddr,sizeof(client_saddr));/*second argument is the queue lenght*/
    if(err == -1)
    {
        perror("connect");
        fflush(stdout);
        exit(EXIT_FAILURE);
    }
    }

    while(1)
    {
        ch=sbuf;
        memset(headbufer,0,sizeof(headbufer));
        AddHeader(sbuf,OUT headbufer);
        wr= write(client_sockfd,headbufer,20);
        memset(headbufer,0,sizeof(headbufer));
        AddHeader(soob,OUT headbufer);
/*      send(client_sockfd,headbufer,20, MSG_OOB);*/
           /* flag = MSG_MORE;
            while(1)
            {
                wr     +=send(client_sockfd,soob,1, MSG_OOB);
                if (wr == BUFSIZE-1)
                {
                    flag =0;
                }
                ++sbuf;

                if(wr == BUFSIZE)
                {
                    wr =0;
                    sbuf = ch;
                    break;
                }
            }
/*         r = read(client_sockfd,buf,BUFSIZE);
 *         printf("%s",buf);
 *         fflush(stdout);
 */
    }
  close(client_sockfd);
  return 0;
}
